import { DataMetadata } from "./editor";
import { project } from "./project";
import * as path from 'path';
import * as fs from 'fs';
import { reloadPreviewBoard } from "./mainboard";



class CurrentData {



    private currentJsonData: any;
    private currentMetadata: any;
    setCurrentMetaData(metadata: DataMetadata) {
        const filename = path.join(project.projectRoot, metadata.filename);
        const content = fs.readFileSync(filename, 'utf-8');
        this.currentJsonData = JSON.parse(content);
        this.currentMetadata = metadata;
    }

    getCurrentData() {
        return this.currentJsonData;
    }

    getCurrentMetaData() {
        return this.currentMetadata;
    }

    saveAndReload() {
        const content = JSON.stringify(this.currentJsonData, null, '\t');
        const filename = path.join(project.projectRoot, this.currentMetadata.filename)
        fs.writeFileSync(filename, content);
        reloadPreviewBoard();
    }
}


export const currentData = new CurrentData();